package com.anhuanjia.tools.eao.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.anhuanjia.tools.eao.bean.EaoBuildingContent;
import com.efastsoft.framework.UIFacade;
import com.efastsoft.framework.annotation.Alias;
import com.efastsoft.framework.annotation.Priv;
import com.efastsoft.framework.annotation.Priv.LoginType;
import com.efastsoft.framework.data.DataRow;
import com.efastsoft.framework.data.DataTable;
import com.efastsoft.framework.data.Q;
import com.efastsoft.framework.data.Transaction;
import com.efastsoft.framework.i18n.Lang;
import com.efastsoft.framework.utility.StringUtil;

/**
 * 对建筑内容操作
 * 
 * @author Administrator
 *
 */
@Alias(value = "buildingcontent")
public class BuildingContentUI extends UIFacade {
	/**
	 * 添加/更改内容表
	 */
	@Priv(loginType = LoginType.Member)
	public void saveOrUpdate() {
		String buildingContentList = $V("BuildingContentList");
		if (StringUtil.isNotEmpty(buildingContentList)) {
			Transaction transaction = new Transaction();
			List<Map<String, Object>> array = JSON.parseObject(buildingContentList,
					new TypeReference<List<Map<String, Object>>>() {
					});
			List<String> bcids = new ArrayList<>();
			String project_id = null;
			if (buildingContentList != null) {
				for (Map<String, Object> list : array) {
					EaoBuildingContent buildingContent = new EaoBuildingContent();
					buildingContent.setValue(list);
					project_id = buildingContent.getProjectId();
					String id = buildingContent.getBcId();
					if (StringUtils.isEmpty(id)) {
						// 添加
						buildingContent.setBcId(UUID.randomUUID().toString());
						transaction.insert(buildingContent);
						bcids.add(buildingContent.getBcId());
					} else {
						// 更新
						bcids.add(id);
						DataTable dt = new Q(" select * from eao_building_content where BcId = ?", id).fetch();
						EaoBuildingContent entity = new EaoBuildingContent();
						entity.setValue(dt.get(0));
						entity.setValue(list);
						transaction.update(entity);
					}
				}
			} else {
				f("传入的值为空");
			}
			DataTable dt = new Q(" select * from eao_building_content where ProjectId = ?", project_id).fetch();
			for (DataRow dataRow : dt) {
				EaoBuildingContent entity = new EaoBuildingContent();
				entity.setValue(dataRow);
				Boolean del = true;
				for (String bcid : bcids) {
					if (entity.getBcId().equals(bcid)) {
						del = false;
					}
				}
				if (del) {
					transaction.delete(entity);
				}
			}
			if (transaction.commit()) {
				s("操作成功");
			} else {
				f("操作失败");
			}
		} else {
			this.Response.put("data", "无操作记录");
		}
	}

	/**
	 * 根据项目ID查询其下的建筑内容
	 */
	@Priv(loginType = LoginType.Member)
	public void findByProjectId() {
		String pid = $V("Id");
		if (StringUtil.isEmpty(pid)) {
			f("查询失败，请确认查询信息");
			return;
		}
		DataTable dt = new Q("select * from eao_building_content where ProjectId = ? ", pid).fetch();
		List<EaoBuildingContent> list = new ArrayList<>();
		for (DataRow dr : dt) {
			EaoBuildingContent entity = new EaoBuildingContent();
			entity.setValue(dr);
			list.add(entity);
			this.Response.put("data", list);
		}
	}

	/**
	 * 查询所有建设内容
	 */
	@Priv(loginType = LoginType.Member)
	public void findAll() {
		DataTable dt = new Q("select * from eao_building_content ").fetch();
		List<EaoBuildingContent> list = new ArrayList<>();
		for (DataRow dr : dt) {
			EaoBuildingContent entity = new EaoBuildingContent();
			entity.setValue(dr);
			list.add(entity);
			this.Response.put("data", list);
		}
	}

	/**
	 * 查询根据ID查询
	 */
	@Priv(loginType = LoginType.Member)
	public void findOne() {
		String searchKey = $V("SearchKey");
		if (StringUtils.isEmpty(searchKey)) {
			f("请输入搜索条件！");
			return;
		}
		Q q = new Q("select * from eao_building_content where BcId = ? ", searchKey);
		$S("data", q.fetch());
	}

	/**
	 * 根据id删除
	 */
	@Priv(loginType = LoginType.Member)
	public void deleteByid() {
		String id = $V("Id");
		if (!StringUtil.isNotEmpty(id)) {
			f("操作失败，请确认操作信息");
			return;
		}
		Transaction trans = new Transaction();
		Q q2 = new Q(" delete from eao_building_content  where ").in("BcId", id);
		trans.add(q2);
		if (trans.commit()) {
			s(Lang.get("Common.ExecuteSuccess"));
		} else {
			f(Lang.get("Common.ExecuteFailed"));
		}
	}

}
